import canvas2image from "./utils/canvas2image.js"


/**
 * 通过当前相机的位置获取关键帧坐标
 * 
 */
export function getKeyFrameOptionByCamera(viewer = window.viewer) {
  const canvas = viewer.scene.canvas
  var genimg = canvas2image.convertToImage(canvas, 291, 185, "png");
  const cartographic = viewer.camera.positionCartographic
  return {
    cameraPose: {
      position: [Cesium.Math.toDegrees(cartographic.longitude), Cesium.Math.toDegrees(cartographic.latitude), cartographic.height],
      heading: Cesium.Math.toDegrees(viewer.camera.heading),
      pitch: Cesium.Math.toDegrees(viewer.camera.pitch),
      roll: Cesium.Math.toDegrees(viewer.camera.roll),
    },
    url: genimg.src
  }
}

/**
 * 飞行到目标
 * @param {*} obj
 */
export function flyTo(option) {
  const flyOption = {
    destination: Cesium.Cartesian3.fromDegrees(...option.position),
    // duration:5, // 设置飞行持续时间，默认会根据距离来计算
  }
  if (option.heading && option.pitch) {
    flyOption.orientation = {
      heading: Cesium.Math.toRadians(option.heading), // 方向
      pitch: Cesium.Math.toRadians(option.pitch), // 倾斜角度
      roll: Cesium.Math.toRadians(option.roll || 0), // 倾斜角度
    }
  }
  viewer.camera.flyTo(flyOption);
}